//
//  MSTradeConfirmHeadView.m
//  SocietySale
//
//  Created by Mike on 16/11/14.
//  Copyright © 2016年 Mike. All rights reserved.
//

#import "MSTradeConfirmHeadView.h"

@interface MSTradeConfirmHeadView ()
@property (nonatomic, strong) UILabel *typeLabel;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *themeLabel;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UILabel *tradePriceLabel;
@property (nonatomic, strong) UILabel *memberTypeLabel;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UIView  *bgView;
@end

@implementation MSTradeConfirmHeadView

-(id)init
{
    self=[super init];
    if (self)
    {
        self.backgroundColor = [UIColor whiteColor];
        self.frame = CGRectMake(0, 0, SCREENWIDTH, 150);
        [self initUI];
    }
    return self;
}

- (void)initUI{
    self.typeLabel = [self creatLabel:CGRectMake(12, 10, SCREEN_WIDTH-24, 25)];
    self.nameLabel = [self creatLabel:CGRectMake(12, self.typeLabel.bottom+5, SCREEN_WIDTH-24, 25)];
    self.themeLabel = [self creatLabel:CGRectMake(12, self.nameLabel.bottom+5, SCREEN_WIDTH-24, 25)];
    self.contentLabel = [self creatLabel:CGRectMake(12, self.themeLabel.bottom+5, SCREEN_WIDTH-24, 25)];
    self.contentLabel.numberOfLines = 0;
    
    _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, self.themeLabel.bottom+10, SCREEN_WIDTH, 40)]; // top:227
//    _bgView.backgroundColor = kDefaultViewBackgroundColor;
    [self addSubview:_bgView];
    
    UIView *horizonLineView =[[UIView alloc] initWithFrame:CGRectMake(12, 0, SCREEN_WIDTH-24, 0.6)];
    horizonLineView.backgroundColor = kNormalGray;
    [_bgView addSubview:horizonLineView];

    self.tradePriceLabel = [self creatLabel2:CGRectMake(12, 10, SCREEN_WIDTH/2, 20)];
    self.memberTypeLabel = [self creatLabel2:CGRectMake(SCREEN_WIDTH/2-20, self.tradePriceLabel.top, 100, 20)];
    self.timeLabel = [self creatLabel2:CGRectMake(SCREEN_WIDTH-92, self.tradePriceLabel.top, 80, 20)];
    self.timeLabel.textAlignment = NSTextAlignmentRight;
}

//创建label
- (UILabel*)creatLabel:(CGRect)frame{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.font = HeitiFont(14);
    label.textColor = kDarkGrayColor;
    [self addSubview:label];
    return label;
}

//创建label
- (UILabel*)creatLabel2:(CGRect)frame{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.font = HeitiFont(14);
    label.textColor = kDarkGrayColor;
    [_bgView addSubview:label];
    return label;
}

- (void)setModel:(MSTradeModel *)model{
    _model = model;
    NSString *tempStr = @"";
    if ([model.type isEqualToString:@"1"]) {//出售
        tempStr = @"出售";
    }
    else{//求购
        tempStr = @"求购";
    }
    NSString *type = [NSString stringWithFormat:@"%@  %@",tempStr,model.projectType];
    self.typeLabel.attributedText =  [self getMutableAttriStrWithForStr:@"类型    " backStreet:type];
    self.nameLabel.attributedText =  [self getMutableAttriStrWithForStr:@"项目    " backStreet:model.title];
    
    if ([model.typeGroup isEqualToString:@"contacts"]) {
        _bgView.top = self.nameLabel.bottom+10;
    }
    else{
        self.themeLabel.attributedText =  [self getMutableAttriStrWithForStr:@"主题    " backStreet:model.theme];
        self.contentLabel.attributedText =  [self getMutableAttriStrWithForStr:@"内容    " backStreet:model.content];
        CGSize size = [HDUtil sizeWithString:model.content font:HeitiFont(14) size:CGSizeMake(SCREEN_WIDTH-70, CGFLOAT_MAX)];
        _bgView.top = self.contentLabel.bottom+10;
    }
    self.height = _bgView.bottom;
    
    
    NSMutableAttributedString *priceAttstr = [[NSMutableAttributedString alloc] initWithString:@"价格     x " attributes:@{NSFontAttributeName:HeitiFont(14),NSForegroundColorAttributeName:kDarkGrayColor}];
    
    NSTextAttachment *textAttachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
    textAttachment.bounds = CGRectMake(0, -4, 17, 17);
    textAttachment.image = [UIImage imageNamed:@"coin"];
    NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:textAttachment];
    [priceAttstr insertAttributedString:attachmentString atIndex:6];
    
    NSAttributedString *priStr = [[NSAttributedString alloc] initWithString:model.tradePrice attributes:@{NSFontAttributeName:HeitiFont(14),NSForegroundColorAttributeName:kLightBlackColor}];
    [priceAttstr appendAttributedString:priStr];
    self.tradePriceLabel.attributedText = priceAttstr;
    
    NSMutableAttributedString *memberAttstr = [[NSMutableAttributedString alloc] initWithString:model.memberType attributes:@{NSFontAttributeName:HeitiFont(14),NSForegroundColorAttributeName:kDarkGrayColor}];
    NSTextAttachment *memberAttachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
    memberAttachment.bounds = CGRectMake(0, -2.5, 15, 12.5);
    memberAttachment.image = [UIImage imageNamed:@"diamond"];
    NSAttributedString *memberAttachmentStr = [NSAttributedString attributedStringWithAttachment:memberAttachment];
    [memberAttstr insertAttributedString:memberAttachmentStr atIndex:6];
    
    self.memberTypeLabel.attributedText = memberAttstr;

    NSMutableAttributedString *timeAttstr = [[NSMutableAttributedString alloc] initWithString:model.time attributes:@{NSFontAttributeName:HeitiFont(13),NSForegroundColorAttributeName:kDarkGrayColor}];
    
    NSTextAttachment *textAttachment2 = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
    textAttachment2.bounds = CGRectMake(0, -2.5, 14, 14);
    textAttachment2.image = [UIImage imageNamed:@"time"];
    NSAttributedString *attachmentString2 = [NSAttributedString attributedStringWithAttachment:textAttachment2];
    [timeAttstr insertAttributedString:attachmentString2 atIndex:0];
    
    self.timeLabel.attributedText = timeAttstr;
}

- (NSMutableAttributedString*)getMutableAttriStrWithForStr:(NSString*)forStr backStreet:(NSString*)backStr{
    NSMutableAttributedString *AttStr = [[NSMutableAttributedString alloc] initWithString:forStr attributes:@{NSFontAttributeName:HeitiFont(14),NSForegroundColorAttributeName:kDarkGrayColor}];
    
    NSAttributedString *backAttStr = [[NSAttributedString alloc] initWithString:backStr attributes:@{NSFontAttributeName:HeitiFont(14),NSForegroundColorAttributeName:[UIColor blackColor]}];
    [AttStr appendAttributedString:backAttStr];
    
    return AttStr;
}

@end
